package com.test.service.impl;

import com.test.controller.Admininfo.MyObject;
import com.test.controller.Admininfo.ResCode;
import com.test.controller.Admininfo.ResData;
import com.test.entity.DepartInfo;
import com.test.dao.DepartInfoDao;
import com.test.service.DepartInfoService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 部门(DepartInfo)表服务实现类
 *
 * @author makejava
 * @since 2022-11-12 11:01:47
 */
@Service("departInfoService")
public class DepartInfoServiceImpl implements DepartInfoService {
    @Resource
    private DepartInfoDao departInfoDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public DepartInfo queryById(Integer id) {
        return this.departInfoDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param departInfo 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public ResData queryByPage(DepartInfo departInfo, PageRequest pageRequest) {
        int total =Integer.parseInt(String.valueOf( this.departInfoDao.count(departInfo)));
        List<DepartInfo> departInfoList = departInfoDao.queryAllByLimit(departInfo, pageRequest);
        int pageSize = pageRequest.getPageSize();
        int page;
        page = (total%pageSize==0)? total/pageSize:total/pageSize+1;
        List<MyObject> myObjectList = departInfoList.stream().map(departInfo1 -> new MyObject(departInfo1)).collect(Collectors.toList());
        return new ResData(ResCode.SUCCESS,myObjectList,total,page);
    }

    /**
     * 新增数据
     *
     * @param departInfo 实例对象
     * @return 实例对象
     */
    @Override
    public int insert(DepartInfo departInfo) {
        return this.departInfoDao.insert(departInfo);
    }

    /**
     * 修改数据
     *
     * @param departInfo 实例对象
     * @return 实例对象
     */
    @Override
    public int update(DepartInfo departInfo) {
        return this.departInfoDao.update(departInfo);
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.departInfoDao.deleteById(id) > 0;
    }

    /**
     * 查询所有部门
     * @return
     */
    @Override
    public List<DepartInfo> selectAll() {
        return departInfoDao.selectAll();
    }
}
